package com.example.initproj.base.exception

import com.example.initproj.base.result.ERROR_UNKNOWN
import com.example.initproj.base.result.Result
import com.example.initproj.base.result.failed
import com.example.initproj.base.util.Log
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

/**
 * 2020/6/30
 * xiang
 * Description:
 **/
@ControllerAdvice
class ExceptionHandler {

    companion object {
        private const val TAG = "ExceptionHandler"
    }

    @ExceptionHandler(Exception::class)
    @ResponseBody
    fun handle(e: Exception): Result {
        val result = if (e is MyException) {
            failed(e.err)
        } else {
            failed(ERROR_UNKNOWN)
        }
        Log.e(TAG, e)
        return result
    }

}
